package microcredit.gui.editor.training;

import java.util.Locale;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import microcredit.gui.editor.AbstractEditorForm;
import microcredit.gui.util.MDatePicker;
import microcredit.gui.util.FixedLengthDocument;
import microcredit.gui.util.UIUtil;
import microcredit.model.MemberToTrainingType;
import net.miginfocom.swing.MigLayout;

public class MemberToTrainingTypeForm extends AbstractEditorForm<MemberToTrainingType> {

	private static final long serialVersionUID = 1L;
	private JLabel lblStatus;
	private JLabel lblDate;
	private MDatePicker datePicker;
	private TrainingStatusCombobox trainingStatusCombobox;
	
	private JLabel lblNotes;
	private JScrollPane scrollPane;
	private JTextArea notesTextArea;
	
	public MemberToTrainingTypeForm() {
		initialize();
		UIUtil.setMandatoryFields(this, MemberToTrainingType.DATE, MemberToTrainingType.STATUS);
	}
	
	@Override
	public void focusDefault() {
		getDatePicker().requestFocus();
	}
	
	@Override
	public void apply(MemberToTrainingType e) throws Throwable {
		getTrainingStatusCombobox().setValue(e.getStatus());
		getDatePicker().setDate(e.getDate());
		getNotesTextArea().setText(e.getNotes());
	}

	@Override
	public MemberToTrainingType receive(MemberToTrainingType e)
			throws Throwable {
		e.setDate(getDatePicker().getDate());
		e.setStatus(getTrainingStatusCombobox().getValue());
		e.setNotes(getNotesTextArea().getText());
		return e;
	}
	
	private void initialize() {
		setLayout(new MigLayout("", "[][right][grow]", "[][][][][grow,top]"));
		add(getLblStatus(), "cell 1 2,alignx trailing");
		add(getTrainingStatusCombobox(), "cell 2 2,growx");
		add(getLblDate(), "cell 1 3");
		add(getDatePicker(), "cell 2 3");
		add(getLblNotes(), "cell 1 4");
		add(getScrollPane(), "cell 2 4,grow");
	}
	private JLabel getLblStatus() {
		if (lblStatus == null) {
			lblStatus = new JLabel("Status:");
		}
		return lblStatus;
	}
	private JLabel getLblDate() {
		if (lblDate == null) {
			lblDate = new JLabel("Date:");
		}
		return lblDate;
	}
	private MDatePicker getDatePicker() {
		if (datePicker == null) {
			datePicker = new MDatePicker();
			datePicker.setLocale(Locale.ENGLISH);
			datePicker.setName(MemberToTrainingType.DATE);
		}
		return datePicker;
	}
	private TrainingStatusCombobox getTrainingStatusCombobox() {
		if (trainingStatusCombobox == null) {
			trainingStatusCombobox = new TrainingStatusCombobox();
			trainingStatusCombobox.enableAutocompletion();
			trainingStatusCombobox.setName(MemberToTrainingType.STATUS);
		}
		return trainingStatusCombobox;
	}
	private JLabel getLblNotes() {
		if (lblNotes == null) {
			lblNotes = new JLabel("Notes:");
		}
		return lblNotes;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getNotesTextArea());
		}
		return scrollPane;
	}
	private JTextArea getNotesTextArea() {
		if (notesTextArea == null) {
			notesTextArea = new JTextArea();
			notesTextArea.setDocument(new FixedLengthDocument(5000));
		}
		return notesTextArea;
	}
}
